package com.mercury.controller;

import com.mercury.exception.MercuryException;
import com.mercury.filter.MercuryFilter;
import com.mercury.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.Part;

/**
 * @author ：tx
 * @description：
 * @date ：Created in 2019/2/27 16:50
 * @modified By：
 * @version:
 */
@Controller
@RequestMapping("/fileController")
public class FileController {

    @RequestMapping("/index")
    public String indexView(){
        return "indexView";
    }

    @RequestMapping(value = "saveFile",method = RequestMethod.POST)
    public String saveFileView(@RequestPart("mercuryFile") Part mercuryFile){
        System.out.println(mercuryFile.getName());
        return "saveFileView";
    }

    //测试错误码
    @RequestMapping(value = "/testCode",method = RequestMethod.GET)
    public String testCode(){
        throw new MercuryException();
        //return "indexView";
    }

    //针对本controller异常做处理
    //针对所有controller异常处理 见ExceprionHandlerAdvice.java
   /* @ExceptionHandler(MercuryException.class)
    public String errorHappend(){
        return "indexView";
    }*/


   //重定向转移参数
    @RequestMapping(value = "/redirectMethod",method = RequestMethod.GET)
    public String redirectMethod(RedirectAttributes model){
        User user = new User("mercury","11");
        model.addFlashAttribute(user);
        model.addAttribute("userName","mercury");
        return "redirect:/fileController/finalMethod/{userName}";
    }

    @RequestMapping(value = "/finalMethod/{userName}")
    public String finalMethod(@PathVariable("userName") String userName, Model model){
        System.out.println(userName);
        return "indexView";
    }

}
